/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package warehouse;

import system.ListType;
import ejb.warehouse.Ordine;
import ejb.warehouse.OrdineFacadeRemote;
import ejb.warehouse.OrdineProdotto;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import system.Utilities;



/**
 *
 * @author ciummo
 */
public class OrderActionForm extends org.apache.struts.action.ActionForm {
    
    
    //Tipi di pagamento
    private final static String sesnov= "60/90";
    private final static String sess ="60";
    private final static String treses = "30/60";
    
    //Controlli per il l'id Ordine
    private final static int minId = 2012000000 ;
    private final static int maxId = 2050000000 ;    
    
    //Variabile che mi serve per non far validare l'id Ordine nella modifica
    private int creation = 1;
    
    
    //Array per caricare i tipi di pagamento nella pagina jsp
    private ArrayList paymentList = new ArrayList();
    
    private List <Ordine> list;
    private List <OrdineProdotto> listOrdineProdotto;
    
    private List <OrderActionForm> listOrderForm;
    
    private List <OrderProductActionForm> listOrderProductForm;
    
    
    private int idOrdine;
    private String dataEvasione;
    private String tipoPagamento;
    private Boolean resoFranco=false;
    private String stato;
    private String note;

    public String getDataEvasione() {
        return dataEvasione;
    }

    public void setDataEvasione(String dataEvasione) {
        this.dataEvasione = dataEvasione;
    }

    public List<OrdineProdotto> getListOrdineProdotto() {
        return listOrdineProdotto;
    }

    public void setListOrdineProdotto(List<OrdineProdotto> listOrdineProdotto) {
        this.listOrdineProdotto = listOrdineProdotto;
    }

    public List<OrderActionForm> getListOrderForm() {
        return listOrderForm;
    }

    public void setListOrderForm(List<OrderActionForm> listOrderForm) {
        this.listOrderForm = listOrderForm;
    }

    public List<OrderProductActionForm> getListOrderProductForm() {
        return listOrderProductForm;
    }

    public void setListOrderProductForm(List<OrderProductActionForm> listOrderProductForm) {
        this.listOrderProductForm = listOrderProductForm;
    }


    

    
    

    public int getIdOrdine() {
        return idOrdine;
    }

    public void setIdOrdine(int idOrdine) {
        this.idOrdine = idOrdine;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Boolean getResoFranco() {
        return resoFranco;
    }

    public void setResoFranco(Boolean resoFranco) {
        this.resoFranco = resoFranco;
    }

    public String getStato() {
        return stato;
    }

    public void setStato(String stato) {
        this.stato = stato;
    }

    public String getTipoPagamento() {
        return tipoPagamento;
    }

    public void setTipoPagamento(String tipoPagamento) {
        this.tipoPagamento = tipoPagamento;
    }
    
    public int getCreation() {
        return creation;
    }

    public void setCreation(int creation) {
        this.creation = creation;
    }
    


    /**
     *
     */
    public OrderActionForm() {
        super();
        
        paymentList.add(new ListType(1,sesnov));
        paymentList.add(new ListType(2,sess));
        paymentList.add(new ListType(3,treses));
        
    }

    public ArrayList getPaymentList() {
        return paymentList;
    }

    public void setPaymentList(ArrayList paymentList) {
        this.paymentList = paymentList;
    }
    
    
    public List<Ordine> getList() {
        return list;
    }

    public void setList(List<Ordine> list) {
        this.list = list;
    }

    public Ordine getElement(int n) {
        return (Ordine)list.get(n);
    }    
    

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        
        Utilities utility = new Utilities();
        
        //Il controllo di validazione dell'id lo effettuo solo nel caso in cui sto creando l ordine
        if (getCreation() == 1){
        
            OrdineFacadeRemote ordiniFacade=null;

            try {
                ordiniFacade = (OrdineFacadeRemote) utility.callRemoteBean("OrdineFacade/remote");
            } catch (NamingException ex) {
                Logger.getLogger(OrderActionForm.class.getName()).log(Level.SEVERE, null, ex);
            }

            try{

                if (!ordiniFacade.find(getIdOrdine()).equals(null)){

                    errors.add("idOrdine", new ActionMessage("error.order.existIdOrder"));
                    }
            }catch(NullPointerException npe){}


            if (getIdOrdine() < minId || getIdOrdine() > maxId){

                errors.add("idOrdine", new ActionMessage("error.order.badIdOrder"));
            }
        }
        
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        
        Date testDate = null;
        
        try {
            testDate = sdf.parse(getDataEvasione());
        }catch(ParseException e){

            errors.add("dataArrivo", new ActionMessage("error.order.badDate"));
        }
        
        if (getTipoPagamento().equals("0")){
            
            errors.add("tipoPagamento", new ActionMessage("error.order.paymentVoid"));
        }
        
        
        
        
        return errors;
    }
    
 	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request){
        
        this.resoFranco = false;
    }
}